package com.cloudtasks;
import com.cloudtasks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ListsDisplayActivity extends Activity {

	String UserName = "";
	long UserID;
	final Context myContext = this;
	private Cursor DBcursor;
	final static String TYPE_PAGE = "list";
	private Database mDb;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groc_lists_page);
        
        try{        	
        	// Add Button ADD
	        Button addItem = (Button)findViewById(R.id.add_list_button);
	        addItem.setOnClickListener(addListListener);
	        
	        // Unwrap parameters from last activity: the User's name
	        // and also his User ID
	        Bundle param = getIntent().getExtras(); 
	        if(param != null){
	        	UserName = param.getString("uname");
		        UserID = param.getLong("uid");
	        }
	        
	        // Change the title of this page to display who's the user
	        TextView pageTitle = (TextView)findViewById(R.id.list_title_textview);
	        pageTitle.setText(UserName + "'s List");
	
	        mDb = new Database(this);
	        mDb.open();
	        ListView lv = (ListView) findViewById(R.id.lists_listview);
	        DBcursor = mDb.fetchAllListsByUser(UserID);
	        
	        lv.setAdapter(new SimpleCursorAdapter(this,  
	        	    android.R.layout.simple_list_item_1,
	        	    DBcursor,
	        	    new String[] {Database.KEY_NAME},
	        	    new int[] { android.R.id.text1}
	        	));
	        startManagingCursor(DBcursor);
	        
	        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {  
						@Override
						public void onItemClick(AdapterView<?> av, View view,
								int pos, long id) {
							String value = ((TextView) view).getText().toString();
							ListView lv = (ListView) findViewById(R.id.lists_listview);	  //why is this needed, it's not      		
							Intent intent = new Intent(myContext, OptionTabsInitializer.class);
							intent.putExtra("uname", UserName);
							intent.putExtra("uid", UserID);
							intent.putExtra("listname", value);
							intent.putExtra("lid", lv.getAdapter().getItemId(pos));
							startActivity(intent);   
						}  
	        	      });  
	        lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener(){ 
	        	@Override 
	        	public boolean onItemLongClick(AdapterView<?> av, View view, int pos, long id) { 
	        		CustomDialog editPopup = new CustomDialog(myContext, mDb, DBcursor, "edit", TYPE_PAGE);
	        		ListView lv = (ListView) findViewById(R.id.lists_listview);	        		
	        		editPopup.passNameID(lv.getAdapter().getItemId(pos));
	        		editPopup.show();
					return false;
	        	} 
	        }); 
        }
        catch(Exception e){
        	MyTools.displayToastMessage(myContext, e.toString());
        }
    }
    
    /////////////// CLICK LISTENERS /////////////////
    /* Add Listener */
    private OnClickListener addListListener = new OnClickListener(){
		public void onClick(View v){						
			// fetch the name of the user				
			try{
				CustomDialog addPopup = new CustomDialog(myContext, mDb, DBcursor, "add", TYPE_PAGE);
				addPopup.passNameID(UserID);
				addPopup.show();
			}
			catch(Exception e){
				MyTools.displayToastMessage(myContext,e.toString());	
			}
		}
	};
	
	public void onDestroy() {
        super.onDestroy();
     
        mDb.close();
    }
}